<?php
/**
 * 车辆状态修复
 * @author angl
 */
die("exit");
date_default_timezone_set("Asia/Shanghai");
set_time_limit(0);
if (isset($_SERVER['REQUEST_URI'])) {
    die('error');
}

if (!defined('API_ROOT_PATH')) {
    define('API_ROOT_PATH', dirname(__FILE__));
}

require(API_ROOT_PATH . '/../inc/common.php');
require(API_ROOT_PATH . '/../inc/Config.php');
require(API_ROOT_PATH . '/../inc/MysqliDb.php');

//初始化数据库链接
function init_db()
{
    $db = new MysqliDb ([
        'host' => MYSQLDB,
        'username' => MYSQLDB_USER,
        'password' => MYSQLDB_PWD,
        'db' => MYSQLDB_DATABASE,
        'port' => 3306,
        'prefix' => 'au_',
        'charset' => 'utf8'
    ]);
    return $db;
}

//初始化redis
function init_redis()
{
    $redis = new Redis();
    $redis->connect(REDIS_HOST, REDIS_PORT);
    return $redis;
}

$db = init_db();

//$list = M('order')->alias('o')->field($cols)
//    ->join(C('DB_PREFIX') . "pay_log as pl ON o.car_id = pl.car_id")
//    ->select();
$db->join("pay_log as pl", "o.car_id = pl.car_id", "INNER");
$list = $db->get ("order AS  o",null,"pl.pay_type,pl.status as pay_status,pl.car_id");
$i = 1;
foreach($list as $kl=>$vl){
    if($vl['pay_type'] == 1){ 					 //付首款
        if($vl['pay_status'] == 0){
            $pay_status_code = 1;            //待付首款
        }elseif($vl['pay_status'] == 1){
            $pay_status_code = 2;      			 //已付收款
        }
    }elseif($vl['pay_type'] == 2){ 				 //付尾款
        if($vl['pay_status'] == 0){
            $pay_status_code = 3;                //待付尾款
        }elseif($vl['pay_status'] == 1){
            $pay_status_code = 4;                //已付尾款
        }
    }
    $res = $db->where('car_id',$vl['car_id'])->update('cars',[
        'pay_status'=>$pay_status_code
    ]);
    if(!$res){
        logUtil("车辆付款状态｛$vl[car_id]} 处理失败！ ","ERROR");
    }
    logUtil("车辆付款状态｛$vl[car_id]} 处理成功！ ","INFO");
    $i++;
}
logUtil("数据处理完成","INFO");
